package hjj.dwr;

import java.util.ArrayList;
import java.util.List;

import util.MD5Util;

import dao.impl.PowerDAOImpl;
import dao.impl.RoleDAOImpl;
import dao.impl.UserDAOImpl;

import model.Power;
import model.Role;
import model.User;

public class UserDwrManager {
	public List<Role> getAllRole() {
		RoleDAOImpl rdi = new RoleDAOImpl();
		List<Role> list = new ArrayList<Role>();
		list = rdi.getAll();
		return list;
	}

	public boolean updatepassword(String username, String password) {
		UserDAOImpl userdaoimpl = new UserDAOImpl();
		MD5Util md5 = new MD5Util();
		User user = userdaoimpl.getById(username);
		if (null == user) {
			return false;
		}
		String pws = md5.getMD5(password);
		user.setPassword(pws);
		return userdaoimpl.updateUserPassword(user);
	}

	public boolean updateuser(String username, String roleid, String powerid,
			String status) {
		boolean flag = false;
		RoleDAOImpl rdi = new RoleDAOImpl();
		PowerDAOImpl pdi = new PowerDAOImpl();
		UserDAOImpl udi = new UserDAOImpl();
		try {
			int uproleid = Integer.parseInt(roleid);
			int uppowerid = Integer.parseInt(powerid);
			Role role = rdi.getById(uproleid);
			Power power = pdi.getPowerById(uppowerid);
			User user = new User();
			user.setUsername(username);
			user.setRole(role);
			user.setPower(power);
			if ("1".equals(status)) {
				user.setStatus(true);
			} else {
				user.setStatus(false);
			}
			flag = udi.updateUser(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	public boolean adduser(String username, String password, String roleid,
			String powerid, String status) {
		boolean flag = false;
		RoleDAOImpl rdi = new RoleDAOImpl();
		PowerDAOImpl pdi = new PowerDAOImpl();
		UserDAOImpl udi = new UserDAOImpl();
		MD5Util md5 = new MD5Util();
		try {
			int uproleid = Integer.parseInt(roleid);
			int uppowerid = Integer.parseInt(powerid);
			Role role = rdi.getById(uproleid);
			Power power = pdi.getPowerById(uppowerid);
			User user = new User();
			user.setUsername(username);
			String psw = md5.getMD5(password);
			user.setPassword(psw);
			user.setRole(role);
			user.setPower(power);
			if ("1".equals(status)) {
				user.setStatus(true);
			} else {
				user.setStatus(false);
			}
			flag = udi.addUser(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public boolean deleteuser(String username){
		boolean flag = false;
		UserDAOImpl udi = new UserDAOImpl();
		try{
			flag=udi.deleteByUserName(username);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public boolean adminupdatepassword(String username, String oldpassword,String newpassword) {
		UserDAOImpl userdaoimpl = new UserDAOImpl();
		MD5Util md5 = new MD5Util();
		User user = userdaoimpl.getById(username);
		if (null == user) {
			return false;
		}
		String oldpsw=md5.getMD5(oldpassword);
		if(!user.getPassword().equals(oldpsw)){
			return false;
		}
		String newpws = md5.getMD5(newpassword);
		user.setPassword(newpws);
		return userdaoimpl.updateUserPassword(user);
	}
	
}
